/*
package com.whty.ssm.db01.controller;

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.net.URLEncoder;

*/
/**
 * Copyright 2016-2022 tianyu education Corporation, All Rights Reserved
 * http://www.whty.com.cn/
 *
 * @author wangming
 * @version 1.0
 * @date 2020/12/8 20:39
 * @since 1.0
 *//*

//https://blog.csdn.net/xiongyouqiang/article/details/80488202?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1.control
@Controller
public class DownController {
    @RequestMapping("/download")
    public ResponseEntity<byte[]> download(HttpServletRequest request){
        URL resource = com.whty.ssm.controller.DownController.class.getClassLoader().getResource("jfgz.xlsx");
        File file = new File(resource.getFile());
        System.out.println("=========="+file.getName());
        FileInputStream fio = null;
        try {
            fio = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        byte[] bytes = new byte[50*1024];
        try {
            fio.read(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //http头信息  设置一些约束之类的东西
        HttpHeaders headers = new HttpHeaders();
        //设置编码  为了解决中文名称乱码问题
        String filename = "jfgz.xlsx";
             //将编码加到http头信息中
        headers.setContentDispositionFormData("attachment", "abc.xlsx");
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(bytes, headers, HttpStatus.CREATED);
    }
    @RequestMapping("/download2")
    public ResponseEntity<byte[]> download2(HttpServletRequest request){
        //文件所在位置
        URL resource = this.getClass().getClassLoader().getResource("1.txt");
        //获取要下载的文件
        File file = new File(resource.getFile());
        System.out.println("=========="+file.getName());
        byte[] body = null;
        InputStream is = null;
        try {
            is = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            body = new byte[is.available()];
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            is.read(body);
        } catch (IOException e) {
            e.printStackTrace();
        }
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attchement;filename=asdfasdjf.txt");
        HttpStatus statusCode = HttpStatus.OK;
        ResponseEntity<byte[]> entity = new ResponseEntity<>(body, headers, statusCode);
        return entity;
    }
    @RequestMapping("/download3")
    public void downLoadFile(HttpServletResponse response) {
        //文件所在位置
        URL resource = this.getClass().getClassLoader().getResource("jfgz.xlsx");
        //获取要下载的文件
        File file = new File(resource.getFile());
        OutputStream out = null;
        try {
            response.reset();
            response.setContentType("application/octet-stream;charset=Unicode");
            response.setHeader("Content-Disposition", "attachment; filename=jfgz.xlsx");
            out = response.getOutputStream();
            out.write(FileUtils.readFileToByteArray(file));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @RequestMapping("/download4")
    public ResponseEntity<byte[]> download4(HttpServletRequest request) throws IOException {
        //文件所在位置
        URL resource = this.getClass().getClassLoader().getResource("jfgz.xlsx");
        //获取要下载的文件
        File file = new File(resource.getFile());
        HttpHeaders headers = new HttpHeaders();
        String fileName = URLEncoder.encode("积分规则操作明细表.xlsx","utf-8");
//        headers.add("Content-Disposition", "attchement;filename=" + fileName+".xlsx");
        headers.add("Content-Disposition", "attchement;filename=" + fileName);
        HttpStatus statusCode = HttpStatus.OK;
        ResponseEntity<byte[]> entity = new ResponseEntity<>(FileUtils.readFileToByteArray(file), headers, statusCode);
        return entity;
    }
    @RequestMapping("/download5")
    public void downloadExcel(HttpServletResponse response,HttpServletRequest request) {
        OutputStream out = null;
        FileInputStream input = null;
        try {
            //获取要下载的模板名称
            String fileName = "jfgz.xlsx";//ruleConfig.xlsx
            //设置要下载的文件的名称
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            //通知客服文件的MIME类型
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            //获取文件的路径
           */
/* String filePath = getClass().getResource("/static/" + fileName).getPath();
            System.out.println(filePath);*//*

            URL resource = this.getClass().getClassLoader().getResource("jfgz.xlsx");
            //获取要下载的文件
            File file = new File(resource.getFile());
            input = new FileInputStream(file);
            out = response.getOutputStream();

            byte[] b = new byte[2048];
            int len;
            while ((len = input.read(b)) != -1) {
                out.write(b, 0, len);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }finally{
            try {
                if(null != out){
                    out.flush();
                    out.close();
                }
                if(null != input){
                    input.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    @RequestMapping("/downExcel")
    public ResponseEntity<byte[]> downExcel() throws IOException {
        //获取类路径下的Excel模板文件
        URL resource = Thread.currentThread().getContextClassLoader().getResource("jfgz.xlsx");
        File file = new File(resource.getFile());
        //使下载文件中文名不乱码
        String fileName = URLEncoder.encode("积分规则操作明细表.xlsx","utf-8");

        //设置下载文件MIME类型
        HttpHeaders headers = new HttpHeaders();
        headers.setContentDispositionFormData("attachment",fileName);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        HttpStatus statusCode = HttpStatus.OK;
        ResponseEntity<byte[]> entity = new ResponseEntity<>(FileUtils.readFileToByteArray(file), headers, statusCode);
        return entity;
    }
}
*/
